<?php

namespace App\Containers\AppSection\Profile\Tasks;

use App\Containers\AppSection\Profile\Data\Repositories\ProfileRepository;
use App\Ship\Criterias\ThisUserCriteria;
use App\Ship\Criterias\Where;
use App\Ship\Parents\Tasks\Task;
use Auth;

class CheckIfUserCanCreateProfileTask extends Task
{
    protected ProfileRepository $repository;
    private array $countProfileAllowed = [
        'investor' => ['ie' => 1, 'indiv' => 1, 'entity' => -1,],
        'borrower' => ['ie' => 1, 'indiv' => 0, 'entity' => -1,],
    ];

    public function __construct(ProfileRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run($legalForm): bool
    {
        $countProfileAllowed = $this->countProfileAllowed[Auth::user()->getRoleNames()[0]][$legalForm];
        if ($countProfileAllowed === -1) return true;

        $profilesCount = $this->repository->pushCriteria(new Where('legal_form_type', '=', $legalForm))->pushCriteria(new ThisUserCriteria(Auth::id()))->count();

        return $profilesCount < $countProfileAllowed;
    }
}
